package com.cloudfun.campusshare.controller;

import com.cloudfun.campusshare.common.constant.OrderStatus;
import com.cloudfun.campusshare.common.constant.RefundReview;
import com.cloudfun.campusshare.common.constant.RoleType;
import com.cloudfun.campusshare.common.model.vo.OrderRefundAuditVo;
import com.cloudfun.campusshare.common.response.BaseResult;
import com.cloudfun.campusshare.common.response.BaseResultFactory;
import com.cloudfun.campusshare.common.response.PageResult;
import com.cloudfun.campusshare.common.security.AuthRequired;
import com.cloudfun.campusshare.entity.OrderRefundEntity;
import com.cloudfun.campusshare.service.OrderRefundService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(value = "退款订单相关接口")
@RestController
@RequestMapping("v1/order/refund")
public class OrderRefundController {

    @Autowired
    private OrderRefundService orderRefundService;

    @ApiOperation(value = "退款列表", notes = "退款列表")
    @GetMapping("page")
    public BaseResult<PageResult<OrderRefundEntity>> page(Pageable pageable,
                                                    @ApiParam(value = "原支付订单号") @RequestParam(required = false) String orderNo,
                                                    @ApiParam(value = "审核状态") @RequestParam(required = false) RefundReview refundReview) {
        Page<OrderRefundEntity> page = orderRefundService.page(pageable, orderNo, refundReview);
        return BaseResultFactory.produceSuccess(new PageResult(page.getTotalElements(), page.getContent()));
    }

    @ApiOperation(value = "退款审核", notes = "退款审核")
    @PostMapping("audit")
    @AuthRequired(role = {RoleType.ADMIN, RoleType.OPERATIONAL_MERCHANT})
    public BaseResult<OrderRefundEntity> audit(@RequestBody @Validated OrderRefundAuditVo auditVo) {

        return BaseResultFactory.produceSuccess(orderRefundService.audit(auditVo));
    }

    @ApiOperation(value = "退款订单详情", notes = "退款订单详情")
    @GetMapping("get/{id}")
    public BaseResult<OrderRefundEntity> audit(@PathVariable Integer id) {

        return BaseResultFactory.produceSuccess(orderRefundService.get(id));
    }
}
